package com.example.demo.Controller;


import com.example.demo.model.Student;
import com.example.demo.model.Teacher;
import com.example.demo.repository.StudentRepository;
import com.example.demo.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.ArrayList;

import java.util.List;

@Controller

@RequestMapping("student")

public class StudentController {

    @Autowired

    private StudentService studentService;
    /*
    @Autowired
    private StudentRepository studentRepository;
     */



    // 获取学生列表
    @RequestMapping("list")

    public String list(ModelMap modelMap) {

        modelMap.put("studentList", studentService.list());

        return "student/list";

    }

    // 删除学生
    @RequestMapping("delete/{id}")

    public String delete(@PathVariable Integer id) {

        studentService.delete(id);

        return "redirect:/student/list";

    }

    // 保存学生（新增）
    @RequestMapping("save")

    public String save(Student s) {

        try {

            studentService.save(s);

        } catch (Exception e) {

            return "error";

        }

        return "redirect:/student/list";

    }

    // 编辑学生
    @RequestMapping("edit/{id}")

    public String edit(@PathVariable Integer id, ModelMap map) {

        map.put("student", studentService.get(id));

        return "student/edit";

    }

    // 更新学生（修改）
    @RequestMapping("update")

    public String update(Student s) {

        try {

            studentService.update(s);

        } catch (Exception e) {

            return "error";

        }

        return "redirect:/student/list";

    }

    // 搜索学生（按姓名、年龄或电话查询）
    @RequestMapping("search")

    public String search(Student s, ModelMap modelMap) {

        modelMap.put("studentList", studentService.search(s));

        return "student/list";

    }



}